import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:flutter/material.dart';
import 'package:moose_app/ui/values/constants.dart';
import 'package:permission_handler/permission_handler.dart';

class ATHAddressChooseMap extends StatefulWidget {
  @override
  _ATHAddressChooseMapState createState() => _ATHAddressChooseMapState();
}

class _ATHAddressChooseMapState extends State<ATHAddressChooseMap> {
  Map<String, Marker> _initMarkerMap = <String, Marker>{};

  static final LatLng _initLatLng = LatLng(39.909187, 116.397451);

  AMapController _mapController;

  BitmapDescriptor _markerIcon;

  AMapPoi _currentPoi;

  LatLng _currentLatLng;

  MyLocationStyleOptions _myLocationStyleOptions;

  @override
  void initState() {
    super.initState();
    _requestLocationPermission();
//    _createMarkerImageFromAsset();
  }

  ///通过BitmapDescriptor.fromAssetImage的方式获取图片
  Future<void> _createMarkerImageFromAsset() async {
    if (_markerIcon == null) {
      final ImageConfiguration imageConfiguration =
          createLocalImageConfiguration(context);
      BitmapDescriptor.fromAssetImage(
              imageConfiguration, 'assets/images/icon_location.png')
          .then(_updateBitmap);
    }
  }

  void _updateBitmap(BitmapDescriptor bitmap) {
    setState(() {
      _markerIcon = bitmap;
    });
  }

  @override
  void reassemble() {
    super.reassemble();
    _requestLocationPermission();
  }

  void _requestLocationPermission() async {
    PermissionStatus status = await Permission.locationWhenInUse.request();
    if (status == PermissionStatus.granted) {
      // some thing
    }
    print('permissionStatus=====> $status');
  }

  ///添加一个marker
  void _addMarker(LatLng location) {
    setState(() {
      _initMarkerMap = <String, Marker>{};
    });
    final Marker marker = Marker(
      position: location,
      infoWindowEnable: true,
      infoWindow: InfoWindow(title: _currentPoi.name),
      //使用默认hue的方式设置Marker的图标
      icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueOrange),
    );

    //调用setState触发AMapWidget的更新，从而完成marker的添加
    setState(() {
      //将新的marker添加到map里
      _initMarkerMap[marker.id] = marker;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: kSize450,
      width: MediaQuery.of(context).size.width,
      child: _buildAMapWidget(),
    );
  }

  void _onMapCreated(AMapController controller) async {
    _mapController = controller;
//    controller.moveCamera(cameraUpdate)
  }

  void _onLocationChanged(AMapLocation location) {
//    print("_onLocationChanged provider ${location.provider}");
//    print("_onLocationChanged speed ${location.speed}");
    print("_onLocationChanged latLng ${location.latLng}");
//    if (_currentLatLng == null) {
//      this.setState(() {
//        _currentLatLng = location.latLng;
//      });
//    }
    CameraUpdate cameraUpdate = CameraUpdate.newLatLngZoom(location.latLng, 12);
    _mapController.moveCamera(cameraUpdate);
  }

  void _onCameraMoveEnd(CameraPosition cameraPosition) {
    print(cameraPosition.toMap());
  }

  void _onPoiTouched(AMapPoi poi) {
    this.setState(() {
      _currentPoi = poi;
    });
  }

  AMapWidget _buildAMapWidget() {
    return AMapWidget(
        apiKey: AMapApiKey(iosKey: ""),
        initialCameraPosition: CameraPosition(target: _initLatLng, zoom: 18),
        trafficEnabled: true,
        buildingsEnabled: true,
        compassEnabled: true,
        labelsEnabled: true,
        scaleEnabled: true,
        touchPoiEnabled: true,
        rotateGesturesEnabled: true,
        scrollGesturesEnabled: true,
        tiltGesturesEnabled: true,
        zoomGesturesEnabled: true,
        myLocationStyleOptions: _myLocationStyleOptions,
        markers: Set<Marker>.of(_initMarkerMap.values),
        onTap: _addMarker,
        onMapCreated: _onMapCreated,
        onLocationChanged: _onLocationChanged,
        onCameraMoveEnd: _onCameraMoveEnd,
        onPoiTouched: _onPoiTouched);
  }
}
